package com.xsmart.payment.api.wx;

import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.payment.services.WxPaymentService;
import com.xsmart.payment.tools.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信接口
 *
 * @author guojd
 */
@RestController
@RequestMapping("/api")
public class WxPaymentController {
    @Autowired
    WxPaymentService wxPaymentService;

    /**
     * 购物车结算 获取数据
     *
     * @param requestBean 请求对象
     *                    order_id:订单id
     *                    order_sn:订单编号
     *                    uid:用户id
     * @param request     请求对象
     * @return 成功失败
     */
    @RequestMapping(value = "/v1/WxPaymentController/wxpay", method = RequestMethod.POST)
    public ResponseBean wxpay(@ModelAttribute RequestBean requestBean, HttpServletRequest request) {
        return wxPaymentService.wxpay(requestBean);
    }

    /**
     * 预支付
     * @param requestBean
     * @param request
     * @return
     */
    @RequestMapping(value = "/v1/WxPaymentController/getPayParam", method = RequestMethod.POST)
    public ResponseBean getPayParam(@ModelAttribute RequestBean requestBean, HttpServletRequest request) {
        String clientIp =CommonUtil.getClientIp(request);
        return wxPaymentService.getPayParam(requestBean,clientIp);
    }

    /**
     * 支付结果通知
     * @param requestBean
     * @return String
     */
    @RequestMapping(value = "/v1/WxPaymentController/notifyPaymentResult", method = RequestMethod.POST)
    public String notifyPaymentResult(@ModelAttribute RequestBean requestBean) {
        String requestXml=requestBean.getHttpEntity().getBody();
        return wxPaymentService.notifyPaymentResult(requestBean,requestXml);
    }

    /**
     * 查询支付结果
     * @param requestBean
     * @return String
     */
    @RequestMapping(value = "/v1/WxPaymentController/orderQuery", method = RequestMethod.POST)
    public ResponseBean orderQuery(@ModelAttribute RequestBean requestBean) {
        String requestXml=requestBean.getHttpEntity().getBody();
        return wxPaymentService.orderQuery(requestBean);
    }

}
